<?php

namespace App\Http\Controllers\Admin;

use App\Models\Comment;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class CommentsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $model = new Comment();
        $data = $model->where(function($query) use ($request){
            if(!empty($request->article_id)){
                $query->where('comments.article_id', $request->article_id);
            }
            if(!empty($request->nickname)){
                $query->where('comments.nickname','like', "%".trim($request->nickname)."%");
            }
            if(!empty($request->email)){
                $query->where('comments.email', 'like', "%".trim($request->email)."%");
            }
            if(!empty($request->is_show)){
                $query->where('comments.is_show', trim($request->is_show));
            }
        })->get();
        return view('admin/comment/index')->with('data', $data)->with('request',$request);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $olddata = DB::table("comments")->where("id",$id)->first();
        $article = DB::table("articles")->where("id",$olddata->id)->first();
        return view('admin/comment/edit')->with('article', $article)->with('olddata', $olddata);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except(['_token']);
        if($data["reply"] == null){
            $data["reply"] = "";
        }
        $data["is_show"] = 1;
        if (DB::table("comments")->where("id",$id)->update($data)) {
            return redirect('/admin/comment/index');
        } else {
            return redirect('/admin/comment/edit/'.$id);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        DB::table('comments')->where('id', '=', $id)->delete();
        return redirect('/admin/comment/index');
    }

    /**
     * 批量删除
     */
    public function dels(Request $request)
    {
        if($ids = $request->input('ids')){
            DB::table('comments')->whereIn('id', $ids)->delete();
        }
        return redirect('/admin/comment/index');
    }

    /**
     * 批量删除
     */
    public function shenhe(Request $request)
    {
        if($ids = $request->input('ids')){
            DB::table('comments')->whereIn('id', $ids)->update(['is_show'=>1]);
        }
        return redirect('/admin/comment/index');
    }
}
